################################
#	John Henderson
#	Gerrymandering Incumbency 
#		(with Brian Hamel and Aaron Goldzimer)
#		January 1, 2018
#
################################
# code edits that produced by Chen & Rodden (2013), to replicate their 2000 analysis 
#  for competitiveness outcomes                                                 
################################
#  :: produces moments for simulated plans in 2000
# function called by respective plotting functions, 
#  cd_2000-vert, ad_2000-vert, sd_2000-vert                              
#  cd_2000-flip, ad_2000-flip, sd_2000-flip

##################################################################################
######## SECTION 1: THIS SECTION DOWNLOADS ALL REPLICATION DATA ONTO A LOCAL FOLDER
##################################################################################
#rm(list=ls())

#library(stringr)  
#library(foreign)

setwd('~/Dropbox/StateRedistricting/replication/short/')
dir <- paste(getwd(), "/sims2000",sep="")

#####################################################################
######## SECTION 2: CREATE THE ELECTORAL BIAS FILE
#####################################################################

CrossStateFolder <- paste(dir, "",sep="")

simfile <- paste(CrossStateFolder,"/simdata.txt",sep="")
actualfile02 <- paste(CrossStateFolder,"/actual02data.txt",sep="")
actualfile00 <- paste(CrossStateFolder,"/actual00data.txt",sep="")

if(!file.exists(actualfile02)){  
	file.remove(actualfile02)
	file.remove(actualfile00)
	file.remove(simfile)

	x <- read.delim(paste(CrossStateFolder,"/files.txt",sep=""))        
	x=x[-c(which(x$State=='DE' | x$State=='MI' | x$State=='WY' | x$State=='WI')),]
}

STchamber <- function(st, d, type){
	Chamber <- type
	CrossStateFolder <- paste(dir, "/CrossState",sep="")
	datafile <- paste(CrossStateFolder,"/fulldata.txt",sep="")

	folder <- paste(CrossStateFolder,"/",st,sep="")
	dbffile <- paste(folder,"/",st,".dbf",sep="")

	sharesfile <- paste(folder,"/",d,".txt",sep="")

	if (file.exists(sharesfile) & d>1){

		dbf <- read.dbf(dbffile)
		swrepvote <- sum(dbf$mccain, na.rm=TRUE) / (sum(dbf$mccain, na.rm=TRUE) + sum(dbf$obama, na.rm=TRUE))
		McCainVoteShare <- swrepvote

		x <- read.table(sharesfile)
		x1 <- x[,4:dim(x)[2]]

		mov <- x1 - (1-x1)
		mov <- unname(t(mov))

		##now get actual districts' bias
		chamb <- "cong"
		if (type=="SLDL"){chamb <- "h"}
		if (type=="SLDU"){chamb <- "s"}


		##now get my calculations of bias
		dfile <- paste(CrossStateFolder,"/District2002BushGore/",st,"_",type,".txt",sep="")
		dvotes <- read.delim(dfile)
		mov02 <- dvotes$repshare - (1-dvotes$repshare)

		mybias00 <- NA
		mydistricts00 <- NA
		dfile <- paste(CrossStateFolder,"/District2000BushGore/",st,"_",type,".txt",sep="")
		if (file.exists(dfile)){
			dvotes <- read.delim(dfile)
			mov00 <- dvotes$repshare - (1-dvotes$repshare)

		}

		output <- cbind(st, Chamber,d, mov)      	
		output2 <- cbind(st, Chamber,d, mov02)
		output3 <- cbind(st, Chamber,d, mov00)

	}else{
		output <- t(cbind(c(st, Chamber,d, rep(NA,ncol(mov)))))	
		output2 <- t(cbind(c(st, Chamber,d, NA)))	
		output3 <- t(cbind(c(st, Chamber,d, NA)))
	}

	print(output)
	print(output2)

	write.table(as.data.frame(output), simfile, row.names=F, col.names=(!file.exists(simfile)), append=file.exists(simfile), sep="\t")

	write.table(as.data.frame(output2), actualfile02, row.names=F, col.names=(!file.exists(actualfile02)), append=file.exists(actualfile02), sep="\t")

	write.table(as.data.frame(output3), actualfile00, row.names=F, col.names=(!file.exists(actualfile00)), append=file.exists(actualfile00), sep="\t")

	return(NA)
}

if(!file.exists(actualfile02)){ 
	for (i in 1:dim(x)[1]){
		st <- as.character(x$State[i])
		STchamber(st,x$congress[i],"CD") 
		if(i!=9 & i!=11 & i!=15 & i!=19){
			STchamber(st,x$house[i],"SLDL")
		}  
		if(i!=9 & i!=11 & i!=15 & i!=19){
			STchamber(st,x$senate[i],"SLDU")   
		}                      
	}
}      

##############################################################################
########## Code to Produce Data for Later Plotting 
##############################################################################

s01=sims=read.delim(simfile)
e00=elections00=read.delim(actualfile00)
e02=elections02=read.delim(actualfile02)
                       
charts.dir <- paste(getwd(), "/sims2000",sep="") 

states = as.character(sort(unique(sims$st))[table(sims$st)!=1]) 
plans=read.csv(paste(getwd(),'/redist_authority.csv',sep=''),stringsAsFactors=F)

plans00=plans[which(plans[,1]==2000),]
included=array(FALSE,nrow(plans00))
for(i in 1:length(states)){
	included[which(states[i]==plans00[,2])]=TRUE
}

plans00=plans00[included,]
plans00=plans00[order(plans00[,2]),]
states=sort(states)        

plansCD00 = array(NA,length(states))
plansCD00[which(plans00$congress_court==1)]='C'
plansCD00[which(plans00$congress_independent==1)]='I'
plansCD00[which(plans00$congress_politician==1)]='B'
plansCD00[which(is.na(plansCD00) & plans00$congress_legislatue_partycontrol=='split')]='B'
plansCD00[which(is.na(plansCD00) & plans00$congress_legislatue_partycontrol=='D')]='D'
plansCD00[which(is.na(plansCD00) & plans00$congress_legislatue_partycontrol=='R')]='R'
          
#cbind(states,plans00[,2],plansCD00)
     
plansAD00 = array(NA,length(states))
plansAD00[which(plans00$state_court==1)]='C'
plansAD00[which(plans00$state_independent==1)]='I'
plansAD00[which(plans00$state_politician==1)]='B'
plansAD00[which(is.na(plansAD00) & plans00$state_legislature_partycontrol=='split')]='B'
plansAD00[which(is.na(plansAD00) & plans00$state_legislature_partycontrol=='D')]='D'
plansAD00[which(is.na(plansAD00) & plans00$state_legislature_partycontrol=='R')]='R'
          
#cbind(states,plans00[,2],plansCD00)           
      
states_ix=states[c(which(plansCD00=='D'),which(plansCD00=='R'),which(plansCD00=='B'),which(plansCD00=='C'),which(plansCD00=='I'))]
  
ix_cd=which(sims[,2]=='CD')
ix_ad=which(sims[,2]=='SLDL')
ix_sd=which(sims[,2]=='SLDU')
                              
iy_cd=which(e00[,2]=='CD')
iy_ad=which(e00[,2]=='SLDL')
iy_sd=which(e00[,2]=='SLDU')
  
iz_cd=which(e02[,2]=='CD')
iz_ad=which(e02[,2]=='SLDL')
iz_sd=which(e02[,2]=='SLDU')

sims=s01[ix_cd,] 
elections00=e00[iy_cd,]
elections02=e02[iz_cd,]

do.mean=T
if(do.mean==T){
	do.tau=F
} else{
	do.tau=T	
}
 
# produce a quasi-bayesian measure of flip probability :: prior(simulated vote margin) x data(counterfactual flip ratio) 
#load state swing data
load(paste(getwd(),'/stateSwings.Rdata',sep=''))

flipPr=function(temp,state,year_mins,year_maxs,seats=T){
 	# assumes that temp is built from rshare-(1-rshare)
	rshare=(temp+1)/2    
	dshare=1-(temp+1)/2  
	           
	#imagine some feasible counterfactual
	i.state=which(rownames(year_mins)==state)    
	minR=year_mins[i.state,1]/100
	maxR=year_maxs[i.state,1]/100
	minD=year_mins[i.state,2]/100
	maxD=year_maxs[i.state,2]/100  
	
	updat=as.numeric((as.numeric(sign((dshare+abs(maxD-mean(dshare)))-(rshare-abs(minR-mean(rshare))))!=sign(dshare-rshare))+
	as.numeric(sign((rshare+abs(maxR-mean(rshare)))-(dshare-abs(minD-mean(dshare))))!=sign(rshare-dshare)))>0)
	
	if(seats==T){
		return(mean(updat))
	} else{
		dshare+abs(maxD-mean(dshare))
		rshare-abs(minR-mean(rshare))		
   
		rshare+abs(maxR-mean(rshare)) 	
		dshare-abs(minD-mean(dshare))
	}
	 
}     
  
# called by plotting function : next
# throughput to plotting =>